package com.xiaoming.base;

import com.xiaoming.constants.Constants;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author xiaoming
 * @Date 2020/3/9
 * @blame xiaoming
 */
public class BaseApiService {

    @Autowired
    protected BaseRedisService baseRedisService;

    /**
     * 返回失败
     *
     * @param msg
     * @return
     */
    public ResponseBase setResultError(String msg) {
        return setResult(Constants.HTTP_RES_CODE_500, msg, null);
    }

    public ResponseBase setResultErrorCode(int code, String msg) {
        return setResult(code, msg, null);
    }

    /**
     * 返回成功 ,data可传
     *
     * @param data
     * @return
     */
    public ResponseBase setResultSuccess(Object data) {
        return setResult(Constants.HTTP_RES_CODE_200, Constants.HTTP_RES_CODE_200_VALUE, data);
    }


    /**
     * 返回成功 ,data值为null
     *
     * @return
     */
    public ResponseBase setResultSuccess() {
        return setResult(Constants.HTTP_RES_CODE_200, Constants.HTTP_RES_CODE_200_VALUE, null);
    }


    /**
     * 设置通用返回结果
     *
     * @param code
     * @param message
     * @param data
     * @return
     */
    public ResponseBase setResult(Integer code, String message, Object data) {
        ResponseBase responseBase = new ResponseBase();
        responseBase.setCode(code);
        responseBase.setMessage(message);
        if (data != null) {
            responseBase.setData(data);
        }
        return responseBase;

    }
}
